package com.fantastic.web.controller.admin;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.result.ResultData;
import com.fantastic.model.pojo.zjs.Variety;
import com.fantastic.service.zjs.VarietyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/30 15:06
 * @description:
 */
@RestController
@RequestMapping("/variety")
public class VarietyControllerAdmin {
    @Resource
    private VarietyService varietyService;

    /**
     * 查询菜品信息
     * 使用分页插件后，Dao层和service层返回都要用Page替换原来的list集合
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param variety
     * @param pageNum
     * @param pageSize
     * @param sortStatus 排序状态  1为升序 (从小到大) 0为降序(从大到小)
     * @return
     */
    @GetMapping("/listVariety")
    public ResultData listVariety(Variety variety,
                                  @RequestParam(name="page",
                                          defaultValue = "1") int pageNum,
                                  @RequestParam(name="limit",defaultValue = "9999999") int pageSize, Integer sortStatus) {
        PageHelper.startPage(pageNum, pageSize);
        List<Variety> listVariety = varietyService.listVariety(variety, sortStatus);
        PageInfo<Variety> pageInfo = new PageInfo<>(listVariety);
        return new ResultData(RespStatusInfo.OK, pageInfo);
    }


    /**
     * 查询菜品表和类型表关联信息
     *
     * @param varietyTypeId
     * @return
     */
    @GetMapping("/listVarietyVo")
    public ResultData listVarietyVo(Integer varietyTypeId) {
        return new ResultData(RespStatusInfo.OK, varietyService.listVarietyVo(varietyTypeId));
    }

    /**
     * 查询菜品类型、菜品、菜品规格类型
     *
     * @return
     */
    @GetMapping("/listVarietyVarietySpecType")
    public ResultData listVarietyVarietySpecType() {
        return new ResultData(RespStatusInfo.OK, varietyService.listVarietyVarietySpecType());
    }

    /**
     * 查询单菜品信息
     *
     * @param varietyId
     * @return
     */
    @GetMapping("/getVariety")
    public ResultData getVariety(Integer varietyId) {
        return new ResultData(RespStatusInfo.OK, varietyService.getVariety(varietyId));
    }

    /**
     * 添加菜品信息
     *
     * @param varietyFile 菜品图片
     * @param variety
     * @return
     */
    @PostMapping("/insertVariety")
    public ResultData insertVariety(MultipartFile varietyFile, Variety variety) {
        return new ResultData(RespStatusInfo.OK, varietyService.insertVariety(varietyFile, variety));
    }

    /**
     * 修改菜品信息
     *
     * @param varietyFile 菜品图片
     * @param variety
     * @return
     */
    @PostMapping("/updateVariety")
    public ResultData updateVariety(MultipartFile varietyFile, Variety variety) {
        return new ResultData(RespStatusInfo.OK, varietyService.updateVariety(varietyFile, variety));
    }

    /**
     * 删除菜品
     *
     * @param varietyId 菜品id
     * @return
     */
    @PostMapping("/deleteVariety")
    public ResultData deleteVariety(Integer varietyId) {
        return new ResultData(RespStatusInfo.OK, varietyService.deleteVariety(varietyId));
    }

}
